﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using TKB.Types.Attributes;
using TKB.Types.DataSets;
using TKB.DataService;
using System.Data;
using System.Data.SqlClient;
using TKB.Services.Tools;

namespace TKB.Services.BusinessObjects
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [DefaultDatabase("TKBIthalatDB")]
    public class MuayeneBS : BusinessBase
    {
        public void Save(MUAYENEDataSet ds)
        {
            ITKBDataObject dto = GetTKBDataObject();
            dto.Update(ds.MUAYENE);
        }

        public MUAYENEDataSet SelectByMuayeneKey(Guid pMuayeneKey)
        {
            ITKBDataObject dto = GetTKBDataObject();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = @"SELECT 
                                    M.*
                                FROM TKBIthalatDB.dbo.MUAYENE AS M
                                WHERE M.MuayeneKey = @MuayeneKey";
            cmd.Parameters.Add(ParameterBuilder.CreateSqlParameter("@MuayeneKey", SqlDbType.UniqueIdentifier, pMuayeneKey));
            MUAYENEDataSet ds = new MUAYENEDataSet();
            dto.GetRecords(ds.MUAYENE, cmd);
            return ds;
        }

        public void MuayeneAnalizKaydet(MUAYENEDataSet dsMuayene, ANALIZDataSet dsAnaliz)
        {
            ITKBDataObject dto = null;
            dto = GetTKBDataObject(true);

            AnalizBS analiz = this.CreateBusinessObject<AnalizBS>();

            try
            {
                this.Save(dsMuayene);
                analiz.Save(dsAnaliz);

                if (IsRoot)
                    dto.CommitTransaction();
            }
            catch (Exception ex)
            {
                if (IsRoot)
                    if (dto != null)
                        dto.RollbackTransaction();

                throw (ex);
            }
        }

        public DataTable SelectAllMuayene()
        {
            ITKBDataObject dto = GetTKBDataObject();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = @"SELECT TOP(20)
                                    M.MuayeneKey
	                                , M.RaporNo
	                                , CONVERT(VARCHAR(10), M.AnalizBaslamaTarihi, 104) AS AnalizBaslamaTarihi
	                                , CONVERT(VARCHAR(10), M.AnalizBitisTarihi, 104) AS AnalizBitisTarihi
	                                , M.AnalizinAmaci
	                                , M.Cinsi AS NumuneCinsi 
	                                , M.GTIPNo
	                                , M.Miktari
                                FROM TKBIthalatDB.dbo.MUAYENE AS M
                                ORDER BY M.AnalizBaslamaTarihi DESC";
            DataTable dt = new DataTable();
            dto.GetRecords(dt, cmd);
            return dt;
        }

        public DataTable SelectMuayeneRapor(Guid pMuayeneKey)
        {
            ITKBDataObject dto = GetTKBDataObject();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = @"SELECT 	  
                                  M.RaporNo
                                  , CONVERT(VARCHAR(10), M.AnalizBaslamaTarihi, 104) AS AnalizBaslamaTarihi
                                  , CONVERT(VARCHAR(10), M.AnalizBitisTarihi, 104) AS AnalizBitisTarihi
                                  , M.AnalizinAmaci
                                  , CONVERT(VARCHAR(10),M.GeldigiTarih, 104) AS GeldigiTarih
                                  , M.GeldigiSayi
                                  , M.GeldigiSaat
                                  , M.GeldigiDakika
                                  , CONVERT(VARCHAR(10),M.TutanakTarihi, 104) AS TutanakTarihi
                                  , M.TutanakSayi
                                  , M.Cinsi
                                  , CONVERT(VARCHAR(10),M.YaziTarihi, 104) AS YaziTarihi
                                  , M.YaziSayisi
                                  , TF.FirmAdi AS GonderenKurulus
                                  , TFF.FirmAdi AS IthalEdenFirma
                                  , M.Ambalaj
                                  , M.GTIPNo
                                  , CONVERT(VARCHAR(10),M.UretimTarihi , 104) AS UretimTarihi
                                  , CONVERT(VARCHAR(10),M.SonKullanmaTarihi , 104) AS SonKullanmaTarihi
                                  , M.GumrukGirisBeyanNo
                                  , M.SeriPartiNo
                                  , TU.UlkeAdi
                                  , M.Miktari
                                  , TF2.FirmAdi AS UreticiFirma
                                  , TG.GumrukAdi
                                  , CONVERT(VARCHAR(10),M.AlinisTarihi, 104) AS AlinisTarihi
                                  , M.İzinVerilenMiktar
                                  , CONVERT(VARCHAR(10),M.KontrolBelgesiTarihi, 104) AS KontrolBelgesiTarihi
                                  , M.KontrolBelgesiSayisi
                                  , M.IthaliGerceklesenMiktar
                              FROM TKBIthalatDB.dbo.MUAYENE AS M 
	                            LEFT OUTER JOIN TKBIthalatDB.dbo.TT_FIRMA AS TF ON M.GonderenKurulusKey = TF.FirmaKey
	                            LEFT OUTER JOIN TKBIthalatDB.dbo.TT_FIRMA AS TFF ON M.IthalEdenFirmaKey = TF.FirmaKey
	                            LEFT OUTER JOIN TKBIthalatDB.dbo.TT_ULKE AS TU ON M.IthalEdilenUlkeKey = TU.UlkeKey
	                            LEFT OUTER JOIN TKBIthalatDB.dbo.TT_FIRMA AS TF2 ON M.UreticiFirmaKey = TF2.FirmaKey
	                            LEFT OUTER JOIN TKBIthalatDB.dbo.TT_GUMRUK AS TG ON M.AlindigiGumrukKey = TG.GumrukKey 
                                WHERE M.MuayeneKey = @MuayeneKey";
            cmd.Parameters.Add(ParameterBuilder.CreateSqlParameter("@MuayeneKey", SqlDbType.UniqueIdentifier, pMuayeneKey));
            DataTable dt = new DataTable();
            dto.GetRecords(dt, cmd);
            return dt;
        }
    }
}
